<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采购商id" prop="purchaserId">
        <el-input
          v-model="queryParams.purchaserId"
          placeholder="请输入采购商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:purch_info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purch_infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="采购商" align="center" prop="purchaserName" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="营业执照" align="center" prop="businessLicense" />
      <el-table-column label="注册信息" align="center" prop="registration" />
      <el-table-column label="电子合同" align="center" prop="contract" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getInfo(scope.row)"
            v-hasPermi="['system:purch_info:list']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="StatusUpdate(scope.row,1)"
            v-hasPermi="['system:purch_info:edit']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="StatusUpdate(scope.row,2)"
            v-hasPermi="['system:purch_info:edit']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商认证信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购商" prop="purchaserId">
          <el-input v-model="form.purchaserName" placeholder="" readonly="true"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="" readonly="true"/>
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicense">
          <el-button @click="downloadFile(form.businessLicense)">文件下载</el-button>
        </el-form-item>
        <el-form-item label="注册信息" prop="registration">
          <el-button @click="downloadFile(form.registration)">文件下载</el-button>
        </el-form-item>
        <el-form-item label="电子合同" prop="contract">
          <el-button @click="downloadFile(form.contract)">文件下载</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPurch_info, getPurch_info,  updatePurch_info } from "@/api/system/purch_info";
import { getUserProfile } from "@/api/system/user";
export default {
  name: "Purch_info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商认证信息表格数据
      purch_infoList: [],
      //当前用户信息
      user:{},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaserId: null,
        companyName: null,
        businessLicense: null,
        registration: null,
        contract: null,
        createdAt: null,
        updatedAt: null,
        status: 0,
        auditorId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        purchaserId: [
          { required: true, message: "采购商id不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        businessLicense: [
          { required: true, message: "营业执照不能为空", trigger: "blur" }
        ],
        registration: [
          { required: true, message: "注册信息不能为空", trigger: "blur" }
        ],
        contract: [
          { required: true, message: "电子合同不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购商认证信息列表 */
    getList() {
      this.loading = true;
      getUserProfile().then(response => {
        this.user=response.data;
        listPurch_info(this.queryParams).then(response => {
        this.purch_infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        purchaserId: null,
        companyName: null,
        businessLicense: null,
        registration: null,
        contract: null,
        createdAt: null,
        updatedAt: null,
        status: null,
        auditorId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看 */
    getInfo(row) {
      this.reset();
      const id = row.id;
      getPurch_info(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "采购商认证信息";
      });
    },
    //状态修改
    StatusUpdate(row,status){
      getPurch_info(row.id).then(response => {
        this.form = response.data;
        this.form.auditorId=this.user.userId;
        if (this.form.status==0) {
          this.form.status=status;
          this.form.authType="normal";
          if (this.form.id != null) {
            updatePurch_info(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.getList();
            });
          } 
        }
      });
    },
    downloadFile(src) {
      var elemIF = document.createElement('iframe');
      elemIF.src = src;
      elemIF.style.display = 'none';
      document.body.appendChild(elemIF);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/purch_info/export', {
        ...this.queryParams
      }, `purch_info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
